﻿using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Xml.Serialization;
using HeritageAcademy.Shared;

namespace HeritageAcademy.Models.DataModels
{
    [XmlRoot(ElementName = "book-usermetadata", Namespace = "books:shared")]
    public class CourseUserMetadata : ModelBase
    {
        #region Parameters 
        private CourseLocation _furthestReadLocation;
        private DateTime _lastAccess = DateTime.MinValue;
        private CourseLocation _lastReadLocation;
        #endregion

        #region Ctor 
        public CourseUserMetadata()
        {
            Bookmarks = new ObservableCollection<CourseLocation>();
        }
        #endregion

        #region Properties 
        public ObservableCollection<CourseLocation> Bookmarks { get; set; }

        private string FilePath { get; set; }

        public CourseLocation FurthestReadLocation
        {
            get { return _furthestReadLocation; }
            set
            {
                if (_furthestReadLocation == value) return;
                _furthestReadLocation = value;
                RaisePropertyChanged("FurthestReadLocation");
            }
        }

        public DateTime LastAccess
        {
            get { return _lastAccess; }
            set
            {
                if (_lastAccess == value) return;
                _lastAccess = value;
                RaisePropertyChanged("LastAccess");
            }
        }

        public CourseLocation LastReadLocation
        {
            get { return _lastReadLocation; }
            set
            {
                if (LastReadLocation == value) return;
                _lastReadLocation = value;
                RaisePropertyChanged("LastReadLocation");
            }
        }
        #endregion Properties 

        #region Public Methods 
        public static CourseUserMetadata GetFromDisk(string filePath)
        {
            CourseUserMetadata metadata = null;

            if (File.Exists(filePath))
            {
                try
                {
                    var serializer = new XmlSerializer(typeof (CourseUserMetadata));

                    // read from disk
                    TextReader reader = new StreamReader(filePath);
                    metadata = (CourseUserMetadata) serializer.Deserialize(reader);
                    reader.Close();
                }
                catch (Exception)
                {
                    throw new Exception();
                }
            }

            if (metadata == null) 
                metadata = new CourseUserMetadata();

            // set the local file path
            metadata.FilePath = filePath;

            return metadata;
        }

        public void SaveToDisk()
        {
            if (FilePath == null) return;

            var serializer = new XmlSerializer(typeof (CourseUserMetadata));
            TextWriter writer = new StreamWriter(FilePath);
            serializer.Serialize(writer, this);
            writer.Close();
        }
        #endregion Public Methods 
    }
}